package com.sctvcloud.longchang.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.gridsum.tracker.GridsumWebDissector;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.widget.CustomLinearLayout;
import com.ruihang.generalibrary.utils.GeneraLifeObser;
import com.ruihang.generalibrary.utils.GroupOrderHelper;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.longchang.R;
import com.sctvcloud.longchang.beans.FProgram;
import com.sctvcloud.longchang.http.AbsListNetCallback;
import com.sctvcloud.longchang.http.NetUtils;
import com.sctvcloud.longchang.ui.activities.ProgramListActivity;
import com.sctvcloud.longchang.ui.adapter.LookBackAdapter;
import com.sctvcloud.longchang.ui.adapter.ProgramBackAdapter;
import com.sctvcloud.longchang.ui.adapter.holder.ProgramBackHolder;
import com.sctvcloud.longchang.ui.dialog.TimePopupDialog;
import com.sctvcloud.longchang.utils.ListTypeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/7/13 0013.
 * 新闻回放页面
 */
public class ProgramBackFragment extends ColumnBaseFragment implements CanRefreshLayout.OnRefreshListener
        , ProgramBackHolder.OnProgramTitleClickListener, CanRefreshLayout.IScrollView {
    @BindView(R.id.program_back_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView proBack_list;
    //    private LookBackAdapter lookbackAdapter;
    private ProgramBackAdapter programBackAdapter;
    private TimePopupDialog timePopupDialog;
    public static final String TIME_TAG = "dia_time_choice";
    private List<FProgram> mList = new ArrayList<>();
    // private boolean mIsLoading = false;
    // private boolean isFirtShowing = true;

    private String questUrl;
    private boolean isRequesting = false;
    private TvFragment parentFragment;

    public ProgramBackFragment() {
    }

    public void setParentFragment(TvFragment parentFragment) {
        this.parentFragment = parentFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_program_back, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initViews(View v, @Nullable Bundle savedInstanceState) {
//        proBack_list.setGroupIndicator(null);
//        proBack_list.setOnGroupClickListener(this);
//        proBack_list.setOnChildClickListener(this);

        proBack_list.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        proBack_list.setItemAnimator(new DefaultItemAnimator());
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setLoadMoreEnabled(false);
//        scrollLay.setiScrollView(this);
        programBackAdapter = new ProgramBackAdapter(getContext(), null);
        proBack_list.setAdapter(programBackAdapter);
        programBackAdapter.setProgramTitleClickListener(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (null == mList || mList.size() == 0) {
            refreshLayout.autoRefresh();
        }

    }

    @Override
    protected boolean onFragmentShow(boolean isFirst, boolean isCallFromResume) {
        if (null == mList || mList.size() == 0) {
            refreshLayout.autoRefresh();
        }
        return super.onFragmentShow(isFirst, isCallFromResume);
    }

    @Override
    public void onResume() {
        super.onResume();
        GeneraLifeObser.changeLifeState(this, GeneraLifeObser.STATE_RESUME);
    }

    @Override
    public void onDestroyView() {
        if (refreshLayout != null) {
            refreshLayout.setOnRefreshListener(null);
        }
        super.onDestroyView();
    }

    @Override
    public boolean canScrollUp(MotionEvent e) {
        return ViewCompat.canScrollVertically(proBack_list, -1);
    }

    @Override
    public boolean canScrollDown(MotionEvent e) {
        return ViewCompat.canScrollVertically(proBack_list, 1);
    }

    @Override
    public void onRefresh() {
        mList.clear();
        getFirstList();
    }

    /**
     * 请求频道回看新闻数据
     */
    public void getFirstList() {

        isRequesting = true;
        NetUtils.getNetAdapter().getProgramArray(getOwnerName(), questUrl, new AbsListNetCallback<FProgram>(ListTypeUtils.FProgram()) {

            @Override
            public void onSuc(List<FProgram> firstList) {
                if (ListUtils.isListValued(firstList)) {
                    mList = new ArrayList<>();
                    mList.addAll(firstList);
                }
                initData();
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                isRequesting = false;
                super.onError(ex, errorMsg);
            }

            @Override
            public void onEnd() {
                onNetFinish();
            }
        });
    }


    private synchronized void onNetFinish() {
        refreshLayout.refreshComplete();
    }

    private void initData() {
        programBackAdapter.setData(mList);
    }

    @Override
    public void OnProgramTitleClick(FProgram fp) {
        Intent intent = new Intent(getContext(), ProgramListActivity.class);
        intent.putExtra(ProgramListActivity.PROGRAM_ID, fp.getProgrammeId());
        intent.putExtra(ProgramListActivity.PROGRAM_TITLE, fp.getProgrammeName());

        startActivity(intent);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //  if (!mIsLoading) {
            Fragment bf = parentFragment;
            if (parentFragment == null
                    || TextUtils.isEmpty((parentFragment).getProgramBackUrl())) {
                //toast(R.string.loading_no_data);
                onNetFinish();
            } else {
                getFirstList();
            }
            //}
            //isFirtShowing = false;
        }
    }

    public void setQuestUrl(String questUrl) {
        this.questUrl = questUrl;
    }

}
